package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.view.MotionEvent
import android.view.View
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.GoodsDetailsEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.GoodsDetailsContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.GoodsDetailsPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_goods_detials.*

/**
 * Created by JacobHHH on 2018/4/21.
 * 商品详情
 */
class GoodsDetailsActivity : BaseMvpActivity<GoodsDetailsContract.Model,GoodsDetailsContract.View,GoodsDetailsPresenter>(),GoodsDetailsContract.View{

    private lateinit var mGood_id : String

    override fun createPresenter(): GoodsDetailsPresenter = GoodsDetailsPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_goods_detials

    override fun initData() {
        mGood_id = intent.getStringExtra("good_id")
        initView()
        getPresenter().loadData(mGood_id)
    }

    private fun initView() {

    }

    override fun initEvent() {
    }

    override fun setData(data: GoodsDetailsEntity?) {
        fl_loading.visibility=View.GONE
        tv_goods_name.text=data!!.good_name
        ImageLoader.loadToUrl(mContext,iv_goods,Constants.getBaseUrl()+data.good_img)
        tv_intro.text=data.dscp
        tv_price.text="￥"+data.good_price

    }

    override fun onDataFailure(err: String?) {
    }

    private var startY=0.0f
    private var moveY =0.0f
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event!!.action){
            MotionEvent.ACTION_DOWN->{
                startY=event.y
            }
            MotionEvent.ACTION_MOVE->{
                moveY= startY-event.y
            }
            MotionEvent.ACTION_UP->{
                if (moveY<-300){

                    setResult(Constants.ACTIVITY_FINISH_RESULT_CODE)
                    finish()
                    overridePendingTransition(R.anim.translate_in,R.anim.translate_out)
                    return true
                    }
                }
            }
        return false
    }
}